package com.cn.controller;


import cn.hutool.core.bean.BeanUtil;
import com.cn.config.JsonResult;
import com.cn.config.PageResult;
import com.cn.damain.dto.ReserveQuery;
import com.cn.damain.po.StAdmin;
import com.cn.damain.po.StTeacher;
import com.cn.damain.po.StUser;
import com.cn.service.impl.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/list")
    public JsonResult listMsReserve(@RequestBody ReserveQuery pageQuery) {
        PageResult<? extends Object> result = null;
        String userTyp = pageQuery.getUserTyp();
        switch (userTyp) {
            case "管理员":
                result = userService.listAdminData(pageQuery);
                break;
            case "教师":
                result = userService.listTeacherData(pageQuery);
                break;
            case "学生":
                result = userService.listData(pageQuery);
                break;
        }
        userService.listData(pageQuery);
        return JsonResult.result(result);
    }

    @PostMapping("/login")
    public JsonResult login(@RequestBody StUser user) {
        try {
            Object ac  = userService.userLogin(user);
            if(ac == null) {
                throw new Exception("用户名密码错误！");
            }
            return JsonResult.result(ac);
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.failResult(e.getMessage());
        }
    }

    @PostMapping("/add")
    public JsonResult add(@RequestBody StUser user) {
        try {
            userService.saveData(user);
            return JsonResult.result("成功！");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.failResult(e.getMessage());
        }
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody StUser user) {
        try {
            userService.editData(user);
            return JsonResult.result("成功！");
        }catch (Exception e){
            return JsonResult.failResult(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public JsonResult delMsReserve(@RequestBody StUser user) {
        try {
            userService.delData(user.getId(), user.getUserTyp());
            return JsonResult.result("成功！");
        }catch (Exception e){
            return JsonResult.failResult(e.getMessage());
        }
    }
}
